<template>
  <div id="main">
    <div class="table-container">
      <list-table-new ref="listTable" v-bind="tableSetting"></list-table-new>
    </div>
    <dialogPublic
      width="600px"
      :title="title"
      :show.sync="show"
      v-if="show"
      @doSubmit="doSubmit"
      @doClose="() => (show = false)"
    >
      <editForm
        class="form"
        :formConfig="basicConfig"
        ref="basicForm"
        :formData="basicFormData"
      ></editForm>
    </dialogPublic>
  </div>
</template>
<script>
import {
  addInfo,
  deleteInfo,
  updateInfo,
  queryList,
  getExistence,
} from "@/api/businessConfigurationManagement/spotCheckConfiguration.js";
export default {
  data() {
    return {
      accs: [], // 新建多选人员信息
      title: "", // 弹框标题
      show: false, // 弹框显示
      // 表格数据配置
      tableSetting: {
        showIndex: true, // 序号显示
        selectionShow: true, // 多选显示
        showFilterBtn: false, // 过滤器显示
        showCustomBtn: false, // 自定义列显示
        // 搜索框设置
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15,
          },
          searchArr: [
            {
              type: "input",
              label: "用户名",
              model: "name",
              placeholder: "请输入",
            },
            {
              type: "organization",
              label: "所属组织",
              model: "deptName",
              idsStr: "deptId",
              placeholder: "请选择或输入",
            },
          ],
        },
        // 按钮
        actionList: [
          {
            name: "新建",
            icon: "icon-xinjian",
            disabled: !this.$store.getters.isBtnAuth("add"),
            content: "未授权的功能",
            method: () => {
              this.setEditForm(true);
              this.basicFormData = {};
            },
          },
          {
            name: "删除",
            icon: "icon-shanchu",
            disabled: !this.$store.getters.isBtnAuth("delete"),
            content: "未授权的功能",
            method: () => {
              this.delete(0);
            },
          },
        ],
        tableMethods: {
          //点击行的方法
          rowClick: (row) => {},
        },
        // 表头
        column: [
          {
            label: "用户名",
            prop: "name",
            sortable: true,
          },
          {
            label: "所属组织",
            prop: "deptName",
            sortable: true,
          },
          {
            label: "工单抽查优先权（权重值）",
            prop: "workPrior",
            sortable: true,
          },
          {
            label: "两票抽查优先权（权重值）",
            prop: "ticketPrior",
            sortable: true,
          },
          {
            label: "操作",
            type: "button",
            buttonArr: [
              {
                text: "编辑",
                icon: "icon-a-xiugaibianji",
                isShow: () => {
                  return true;
                },
                methods: (row) => {
                  this.setEditForm(false);
                  this.show = true;
                  this.basicFormData = row;
                  this.basicFormData.nameArr = row.name;
                },
              },
              {
                text: "删除",
                icon: "icon-shanchu",
                isShow: () => {
                  return true;
                },
                methods: (row) => {
                  this.delete(1, row);
                },
              },
            ],
          },
        ],
      },
      // 弹框信息表单
      basicConfig: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "人员",
            prop: "name",
            propArr: "nameArr",
            idsStr: "account",
            orgNameStr: "deptName",
            orgIdStr: "deptId",
            required: true,
            span: 24,
            rows: 1,
            multiple: false, //是否多选
            isOutOrg: "", //是否是外单位;0:不是外单位；1是外单位；不做限制就不传此参数
            allowCreate: false, //是否允许创建条目
            option: [], //this.deptOption1
            type: "user-select", //类型
            defaultIds: [],
            trigger: "change",
            methods: (obj) => {
              if (this.title == "新建") {
                this.accs = [];
                obj.forEach((item) => {
                  let data = {
                    account: item.account,
                    deptId: item.deptId || item.orgId,
                    deptName: item.deptName || item.orgName,
                    name: item.name,
                  };
                  this.accs.push(data);
                });
              }
            },
          },
          {
            label: "工单抽查优先权",
            prop: "workPrior",
            type: "number",
            placeholder: "请输入权重值(正整数)",
            methods: async (obj, res) => {
              if (!/(^[1-9]\d*$)/.test(obj)) {
                this.$message.warning("请输入正整数!");
                res.workPrior = "";
                return;
              }
            },
            span: 24,
          },
          {
            label: "两票抽查优先权",
            prop: "ticketPrior",
            type: "number",
            placeholder: "请输入权重值(正整数)",
            methods: async (obj, res) => {
              if (!/(^[1-9]\d*$)/.test(obj)) {
                this.$message.warning("请输入正整数!");
                res.ticketPrior = "";
                return;
              }
            },
            span: 24,
          },
        ],
      },
      // 弹框数据
      basicFormData: {},
    };
  },
  created() {},
  mounted() {
    this.$refs.listTable.searchData();
  },
  methods: {
    // 弹框提交-新增或修改
    async doSubmit() {
      let actionApi = (params) => {
        return this.title == "新建" ? addInfo(params) : updateInfo(params);
      };
      let data = await this.$refs.basicForm.getFormData();
      if (this.title == "新建") {
        data.accs = this.accs;
        delete data.account;
        delete data.deptId;
        delete data.deptName;
        delete data.name;
        delete data.nameArr;
      } else {
        delete data.accs;
      }
      if (data.workPrior || data.ticketPrior) {
        await actionApi(data).then((res) => {
          if (res.code == 200) {
            this.show = false;
            // this.$message.success(res.description);
            this.$message.success("保存成功！");
            this.$refs.listTable.searchData();
          }
        });
      } else {
        this.$message.warning("工单抽查优先权和两票抽查优先权不能同时为空！");
      }
    },
    // type批量0或单项1删除
    delete(type, row) {
      let data = {};
      if (type) {
        data = { ids: row.id };
      } else {
        let arr = this.$refs.listTable.getSelectData();
        let list = [];
        for (let i = 0; i < arr.length; i++) {
          list.push(arr[i].id);
        }
        data = { ids: list.toString() };
      }
      if (data.ids.length != 0) {
        this.$confirm("确定要删除这条数据吗", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          deleteInfo(data).then((res) => {
            if (res.code == 200) {
              // this.$message.success(res.description);
              this.$message.success("删除成功！");
              this.$refs.listTable.searchData();
            }
          });
        });
      } else {
        this.$message.warning("请至少勾选一条数据");
      }
    },
    // 获取表格数据
    async getTableData(params, sortStr) {
      let queryData = Object.assign({}, params);
      if (sortStr.length > 0) {
        queryData.orders = sortStr;
      }
      let { data } = await queryList(queryData);
      this.$set(this.tableSetting, "tableData", data.list || []);
      this.$set(this.tableSetting, "totalNumber", data.total || 0);
    },
    // 新建或编辑设置弹框(flag true/false)
    setEditForm(flag) {
      this.basicConfig.itemArr[0].multiple = flag;
      if (flag) {
        getExistence().then((res) => {
          this.basicConfig.itemArr[0].defaultIds = res.data;
        });
        this.basicConfig.itemArr[0].rows = 3;
        this.title = "新建";
      } else {
        this.basicConfig.itemArr[0].rows = 1;
        this.title = "编辑";
      }
      this.show = true;
    },
  },
};
</script>

<style lang="less" scoped>
#main {
  width: 100%;
  height: 100%;
  display: flex;
  .table-container {
    width: 100%;
    height: calc(100% - 10px);
    padding: 20px;
    /deep/ .kr-list-table-box .kr-table-pagination {
      padding-top: 0;
    }
  }
  .form {
    width: 450px;
    margin: 0 auto;
    padding: 25px 0;
  }
}
</style>